package org.example.business;

import org.example.manage.UserManage;
import org.example.model.User;
import org.example.ui.administrator.ManageUserItem;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class UserManageBusiness {
    public static JPanel userPanel;
    public static ManageUserItem[] manageUserItems;
    public static SpringLayout userLayout;
    public static List<User> list;
    public static void setUserItems() {
        UserManage manage = new UserManage();
        list = manage.selectAll();
        userPanel.removeAll();
        manageUserItems = new ManageUserItem[list.size()];
        for (int i = 0; i < manageUserItems.length; i++) {
            manageUserItems[i] = new ManageUserItem(list.get(i), i + 1);
            userPanel.add(manageUserItems[i]);
            if (i == 0) {
                userLayout.putConstraint(SpringLayout.NORTH, manageUserItems[i], 0, SpringLayout.NORTH, userPanel);
                userLayout.putConstraint(SpringLayout.SOUTH, manageUserItems[i], 100, SpringLayout.NORTH, userPanel);
            }
            else {
                userLayout.putConstraint(SpringLayout.NORTH, manageUserItems[i], 0, SpringLayout.SOUTH, manageUserItems[i-1]);
                userLayout.putConstraint(SpringLayout.SOUTH, manageUserItems[i], 100, SpringLayout.SOUTH, manageUserItems[i-1]);
            }
            userLayout.putConstraint(SpringLayout.WEST, manageUserItems[i], 0, SpringLayout.WEST, userPanel);
            userLayout.putConstraint(SpringLayout.EAST, manageUserItems[i], 0, SpringLayout.EAST, userPanel);
        }
        userPanel.setPreferredSize(new Dimension(300, 105 * list.size()));
        userPanel.setVisible(false);
        userPanel.setVisible(true);
        manage.close();
    }
}
